package com.cnhis.cloudhealth.clinical.clidoctor.treatment.bo;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.clidoctor.treatment.dao.TreatmentDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import java.util.HashMap;
import com.cnhis.cloudhealth.commons.utils.Page;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;

@Service
public class TreatmentBo
{
    @Resource
    private TreatmentDao treatmentDao;
    
    @Autowired
    private GetNewId getNewId;
    
    
    @SuppressWarnings("unchecked")
    public ModelVo queryPatientTreatmentRecord(Map map, int currPage, int pageSize)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        Page pg = null;
        Long value = null;
        try
        {
            if ("1".equals(map.get("acf01").toString()))
            {
                value = treatmentDao.getVac1Value(map);
                map.put("vaa01", value);
                pg = treatmentDao.queryPatientTreatmentRecord(map, currPage, pageSize);
            }
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setList(pg.getList());
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setTotal(pg.getTotalNumber());
        mo.setRows(pg.getList());
        return mo;
    }
    
    public ModelVo getTreatmentItemById(Map map)
        throws Exception
    {
        ModelVo modelVo = new ModelVo();
        List list = null;
        try
        {
            list = treatmentDao.getTreatmentItemById(map);
        }
        catch (Exception e)
        {
            modelVo.setResult(StaticKeys.OH_FALSE);
            return modelVo;
        }
        
        modelVo.setList(list);
        modelVo.setRows(list);
        modelVo.setResult(StaticKeys.OH_SUCCESS);
        return modelVo;
    }
    
    @SuppressWarnings("unchecked")
    public ModelVo insertDoctorDiagnose(Map map)
        throws Exception
    {
        ModelVo modelVo = new ModelVo();
        try
        {
            map.put("newID", getNewId.nextId());
            treatmentDao.insertDoctorDiagnose(map);
        }
        catch (Exception e)
        {
            modelVo.setResult(StaticKeys.OH_FALSE);
            return modelVo;
        }
        
        modelVo.setResult(StaticKeys.OH_SUCCESS);
        return modelVo;
    }
    
    private boolean BDJ1Count(Map map)
        throws Exception
    {
        Integer cout = treatmentDao.BDJ1Count(map);
        if (cout > 0)
        {
            return true;
        }
        return false;
    }
    
    private void updateBDJ1(Map map)
        throws Exception
    {
        if ("0".equals(map.get("Flag").toString()))
        {
            treatmentDao.updateBDJ1Param1(map);
        }
        if ("1".equals(map.get("Flag").toString()))
        {
            treatmentDao.updateBDJ1Param2(map);
        }
    }
    
    @SuppressWarnings("unchecked")
    private void insertBDJ1(Map map)
        throws Exception
    {
        map.put("lID", getNewId.nextId());
        if ("0".equals(map.get("Flag").toString()))
        {
            treatmentDao.insertBDJ1Param1(map);
        }
        if ("1".equals(map.get("Flag").toString()))
        {
            treatmentDao.insertBDJ1Param2(map);
        }
    }
    
    public ModelVo execClinicBDJ1Update(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
        if (BDJ1Count(map))
        {
            updateBDJ1(map);
        }
        else
        {
            insertBDJ1(map);
        }
        if ("1".equals(map.get("Flag").toString()))
        {
            treatmentDao.deleteBDJ1(map);
        }
        return mo;
    }
    
    public ModelVo personTreamentList(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        List ls = null;
        try
        {
            ls = treatmentDao.personTreamentList(map);
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setList(ls);
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setRows(ls);
        return mo;
    }
    
    public ModelVo jibingList(HashMap hs)
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            list = treatmentDao.jibingList(hs);
            // 构造树形结构数据
//            Set<String> set1 = new HashSet<String>();
            List<Map<String, Object>> treeRoot = new ArrayList<Map<String, Object>>();
            for (Map<String, Object> temp : (List<Map<String, Object>>)list)
            {
                // 根节点
//                set1.add(temp.get("id").toString());
                Object parentid = temp.get("pid");
                if (parentid == null || parentid!=null && Integer.parseInt(parentid.toString())==0)
                {
                    // 子节点(递归)
                    configTreeNode(list, temp);
                    treeRoot.add(temp);
                }
            }
            mo.setList(treeRoot);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }
    
    private void configTreeNode(List<Map<String, Object>> ls, Map<String, Object> temp)
    {
        Set<Map<String, Object>> treeNode = new HashSet<Map<String, Object>>();
        Set<String> set = new HashSet<String>();
        for (Map<String, Object> temp1 : (List<Map<String, Object>>)ls)
        {
            if ((temp1.get("pid") != null&&!"0".equals(temp1.get("pid").toString())) && temp1.get("pid").equals(temp.get("id")) && !set.contains(temp1.get("id").toString()))
            {
                treeNode.add(temp1);
                if (temp1.get("id")!=null)
                {
                    set.add(temp1.get("id").toString());
                }
                configTreeNode(ls, temp1);
            }
        }
        temp.put("text", temp.get("name"));
        temp.put("nodes", treeNode);
    }
    
    public ModelVo jibingInfo(HashMap hs)
    {
        ModelVo mo = new ModelVo();
        List ls = null;
        try
        {
            ls = treatmentDao.jibingInfo(hs);
            mo.setList(ls);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo otherPinci(HashMap hs)
    {
        ModelVo mo = new ModelVo();
        List ls = null;
        try
        {
            ls = treatmentDao.otherPinci(hs);
            mo.setList(ls);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo geiyaokeshiList(HashMap hs)
    {
        ModelVo mo = new ModelVo();
        List ls = null;
        try
        {
            ls = treatmentDao.geiyaokeshiList(hs);
            mo.setList(ls);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo loadBce1(HashMap hs)
    {
        ModelVo mo = new ModelVo();
        List ls = null;
        try
        {
            ls = treatmentDao.loadBce1(hs);
            mo.setList(ls);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo handleAdmission(HashMap hs)
    {
        ModelVo mo = new ModelVo();
        List ls = null;
        try
        {
            ls = treatmentDao.handleAdmission(hs);
            mo.setList(ls);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo saveRuyuan(HashMap hs)
    {
        ModelVo mo = new ModelVo();
        List ls = null;
        try
        {
            Map<String,Object> vbn1 = JsonUtil.getMap(hs.get("vbn1").toString());
            Map<String,Object> vaa1 = JsonUtil.getMap(hs.get("vaa1").toString());
            hs.put("vaa1", vaa1);
            treatmentDao.update("Treatment.updateVaa1Byvaa01", hs);
            List vao1List = JsonUtil.getList(hs.get("vao1").toString());
            List<Long> idList = getNewId.nextId(vao1List.size());
            for(int i = 0;i<vao1List.size();i++)
            {
                Map temp = (Map)vao1List.get(i);
                temp.put("vao01", idList.get(i));
            }
            vbn1.put("vbn01", getNewId.nextId());
            hs.put("vbn1", vbn1);
            hs.put("vao1List", vao1List);
            treatmentDao.addVao1(hs);
            
            //
            List tempList = treatmentDao.queryForList("Treatment.hasVbn", hs);
            if(tempList!=null && tempList.size()>0)
            {
                treatmentDao.update("Treatment.updateVbn", hs);
            }
            else
            {
                treatmentDao.insert("Treatment.insertVbn", hs);
            }
            mo.setList(ls);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

	public ModelVo saveZhenduanShouye(HashMap hs) {
		ModelVo mo = new ModelVo();
        List ls = null;
        try
        {
        	List tempList = JsonUtil.getList(hs.get("vao1").toString());
        	List<Long> vao01List = getNewId.nextId(tempList.size());
        	for(int i =0;i<tempList.size();i++)
        	{
        		((Map)tempList.get(i)).put("vao01", vao01List.get(i));
        	}
        	hs.put("vao1List", tempList);
            treatmentDao.saveZhenduanShouye(hs);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
	}

	public ModelVo getZhenduanShouye(HashMap hs) {
		ModelVo mo = new ModelVo();
        List ls = null;
        try
        {
            ls = treatmentDao.getZhenduanShouye(hs);
            mo.setList(ls);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
	}

    public ModelVo queryVbn1ByVaa07(HashMap hs)
    {
        ModelVo mo = new ModelVo();
        List ls = null;
        try
        {
            ls = treatmentDao.queryVbn1ByVaa07(hs);
            mo.setList(ls);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo getZhenduanShouye1(HashMap hs)
    {
        ModelVo mo = new ModelVo();
        List ls = null;
        try
        {
            ls = treatmentDao.getZhenduanShouye1(hs);
            mo.setList(ls);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo saveZhenduanShouye1(HashMap hs)
    {
        ModelVo mo = new ModelVo();
        List ls = null;
        try
        {
            List tempList = JsonUtil.getList(hs.get("vao1").toString());
            List<Long> vao01List = getNewId.nextId(tempList.size());
            for(int i =0;i<tempList.size();i++)
            {
                ((Map)tempList.get(i)).put("vao01", vao01List.get(i));
            }
            hs.put("vao1List", tempList);
            treatmentDao.saveZhenduanShouye1(hs);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo deleteVao1ByVao01(HashMap hs)
    {
        ModelVo mo = new ModelVo();
        List ls = null;
        try
        {
            treatmentDao.deleteVao1ByVao01(hs);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }

    public ModelVo getCailiaoInfoBybbx01(HashMap hs)
    {
        ModelVo modelVo = new ModelVo();
        List list = null;
        try
        {
            list = treatmentDao.getCailiaoInfoBybbx01(hs);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            modelVo.setResult(StaticKeys.OH_FALSE);
            return modelVo;
        }
        modelVo.setList(list);
        modelVo.setRows(list);
        modelVo.setResult(StaticKeys.OH_SUCCESS);
        return modelVo;
    }

    public ModelVo queryAbj1(Map hs)
    {
        ModelVo modelVo = new ModelVo();
        try
        {
            List list = treatmentDao.queryForList("Treatment.queryAbj1", hs);
            List row = treatmentDao.queryForList("Treatment.queryAbj1ByVaa01", hs);
            modelVo.setList(list);
            modelVo.setRows(row);
            modelVo.setResult(StaticKeys.OH_SUCCESS);
            return modelVo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            modelVo.setResult(StaticKeys.OH_FALSE);
            return modelVo;
        }
        
    }

    public ModelVo yibaoJibingList(Map hs)
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            list = treatmentDao.yibaoJibingList(hs);
            mo.setList(list);
            mo.setResult(StaticKeys.OH_SUCCESS);
            return mo;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            mo.setList(list);
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
    }
}
